﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Inventory.Business.API;
using API = Inventory.Business.API;

namespace Inventory.Test
{
    /// <summary>
    /// Summary description for ProgressBilling
    /// </summary>
    [TestClass]
    public class ProgressBillingTest
    {
        public ProgressBillingTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestCreateProgressBilling()
        {
            #region Construct Managers
            var cntMan = new Contract();
            Assert.IsNotNull(cntMan, "Unable to Construct API.Contract");

            var PBMan = new API.ProgressBilling();
            Assert.IsNotNull(PBMan, "Unable to Construct API.ProgressBilling");
            #endregion
            int itemCount;
            #region Get vaild Contract
            var contractList = cntMan.GetList(null, null, false, out itemCount);
            Assert.IsNotNull(contractList, "not Contract found");
            Assert.AreNotEqual(0, contractList.Count, "not Contract found");

            //get first vaild contract
            var contract = contractList.FirstOrDefault(c =>
            {
                return c.CanDoProgressBilling &&
                    c.ContractItems.Count > 0 &&
                    c.ContractPaymentTerms.Count > 0 &&
                    c.ContractItems.Where(i => i.Quantity > 0).Count() == c.ContractItems.Count;
            });
            Assert.IsNotNull(contract, "no Contract valid to create progressBilling found");
            #endregion

            var pb = PBMan.InitialProgressBilling(contract.ID);

            foreach (var pbi in pb.ProgressBillingItemCollection)
                foreach (var item in pbi.ProgressBillingItems)
                {
                    item.TotalQuantityDone += 2;
                    item.QuantityHeld += 1;
                }

            PBMan.Save(pb);
            Assert.AreNotEqual(0, pb.ID);

            var contractAfterPB = cntMan.GetById(contract.ID);
            Assert.IsFalse(contractAfterPB.CanDoProgressBilling);

            var loadedPB = PBMan.GetById(pb.ID);
            Assert.IsNotNull(loadedPB);
            Assert.AreEqual(loadedPB.ProgressBillingItemCollection.ToProgressBillingItemList().Count, contractAfterPB.ContractItems.Count);
        }

        [TestMethod]
        public void TestEditProgressBilling()
        {
            var PBMan = new API.ProgressBilling();

            var pb = PBMan.GetById(5);

                foreach (var item in pb.ProgressBillingItemCollection[0].ProgressBillingItems)
                {
                    item.TotalQuantityDone += 2;
                    item.QuantityHeld += 1;
                }

                PBMan.Save(pb);

                var pbAfterSave = PBMan.GetById(5);

                for (int i = 0; i < pb.ProgressBillingItemCollection[0].ProgressBillingItems.Count; i++)
                    Assert.AreEqual(pb.ProgressBillingItemCollection[0].ProgressBillingItems[i].CompletionPercentage,
                            pbAfterSave.ProgressBillingItemCollection[0].ProgressBillingItems[i].CompletionPercentage);
        }

        [TestMethod]
        public void TestGetById()
        {
            var PBMan = new API.ProgressBilling();

            var loadedPB = PBMan.GetById(5);
            Assert.IsNotNull(loadedPB);
            Assert.AreNotEqual(0, loadedPB.ProgressBillingItemCollection.ToProgressBillingItemList().Count);
        }
    }
}
